#include "geogeometry.h"
#include "src/Geo/utility/geo_convert.h"
#include <algorithm>

GeoPoint::GeoPoint() :
    x(0.0f), y(0.0f)
{
}

GeoPoint::GeoPoint(double xx, double yy) :
    x(xx), y(yy)
{
}

GeoGeometry* GeoPoint::copy() {
    return (new GeoPoint(this->x, this->y));
}

GeometryType GeoPoint::getGeometryType() const
{
    return kPoint;
}

const char* GeoPoint::getGeometryName() const
{
    return "POINT";
}

int GeoPoint::getNumPoints() const
{
    return 1;
}

GeoExtent GeoPoint::getExtent() const
{
    return { x, x, y, y };
}

bool GeoPoint::isEmpty() const
{
    return false;
}

void GeoPoint::swapXY()
{
    std::swap(x, y);
}

void GeoPoint::offset(double xOffset, double yOffset) {
    x += xOffset;
    y += yOffset;
}

//GeoPoint-->OGRPoint
void GeoPoint::importFromWktPoint(const char *wkt)
{

    OGRPoint *poPoint=new OGRPoint();
    poPoint->importFromWkt(&wkt);
    convertOGRPoint(poPoint,this);
    //this->setXY(poPoint->getX(),poPoint->getY());
    /*
    OGRPoint ogrPoint;
    ogrPoint.importFromWkt(&wkt);
    this->setXY(ogrPoint.getX(),ogrPoint.getY());
    */

}

QString GeoPoint::exportToWktPoint()
{
    OGRPoint *poPoint=new OGRPoint();
    QString wktFormat;
    if(convertGeoPoint(this,poPoint))
    {
        char *wkt;
        poPoint->exportToWkt(&wkt,OGRwkbVariant::wkbVariantOldOgc);
        wktFormat=QString(wkt);
        //wktFormat=QString(poPoint->exportToWkt(&wkt,OGRwkbVariant::wkbVariantOldOgc));
    }
    return wktFormat;
    /*
    OGRPoint ogrPoint;
    ogrPoint.setX(this->x);
    ogrPoint.setY(this->y);
    char* wkt;
    QString wktFromat=QString(ogrPoint.exportToWkt(&wkt,OGRwkbVariant::wkbVariantOldOgc));
    */
}

QByteArray GeoPoint::exportToWktByteArrayPoint()
{
    QString wkt_str=this->exportToWktPoint();
    QByteArray result=wkt_str.toLocal8Bit();
    return result;
}

